﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Business_Objects;
using ServicesComponent.Repositories;

public partial class DanhMucChung_LopDetails : System.Web.UI.Page
{
    private IClassRepository _ClassRepository;
    private IBlockRepository _BlockRepository;
    private static string id = "";
    private static string action;
    protected void Page_Load(object sender, EventArgs e)
    {
        _ClassRepository = FactoryServices.GetServicesClass(_ClassRepository);
        _BlockRepository = FactoryServices.GetServicesBlock(_BlockRepository);
        action = Request.QueryString["ac"].ToString();
        id = Request.QueryString["id"].ToString();
        if (IsPostBack == false)
        {
            ddlKhoi.DataSource = _BlockRepository.GetBlockBySchoolId(int.Parse(Session["SchoolID"].ToString()));
            ddlKhoi.DataTextField = "Name";
            ddlKhoi.DataValueField = "Id";
            ddlKhoi.DataBind();
            //Dropdownlist.selectValue.tostring(); //tostring cho nó về kiểu string
            if (action == "'sua'")
            {
                lblTitle.Text = "Chi tiết lớp học";
                FillDataToControl(id);
            }
            if (action == "'them'")
            {
                lblTitle.Text = "Thêm mới lớp học";
            }
        }
    }
    void FillDataToControl(string Id)
    {
        ////Đổ data lên control theo id get về
        Class obj = _ClassRepository.Get(id);
        txtMaLop .Value = obj.ClassNumber ;
       txtTenLop.Value = obj.Name;
        ddlKhoi.SelectedValue = obj.BlockID.ToString();
        chk24.Checked = obj.NhomDuoi24thang;
        chk36.Checked  = obj.NhomTu24den36thang;
        chk2DoTuoi.Checked = obj.Hoc2buoi;
        chk3Maugiao.Checked = obj.Nhatre;

        chk4.Checked = obj.Lop3den4tuoi;
        chk5.Checked = obj.Lop4den5tuoi;
        chk6.Checked = obj.Lop5den6tuoi;
        chkCTGDMoi.Checked = obj.ThuchienCTGDMNmoi;



    }
    bool ValiDate()
    {
        //if (txtMaKhoi.Value.ToString() == "" || txtTenKhoi.Value.ToString() == "")
        //{
        //    return false;
        //}
        return true;
    }


    protected void btnUpdate_Click(object sender, EventArgs e)
    {

        if (ValiDate())
        {

            if (action == "'sua'")
            {
                Guid iGuid = Guid.Parse(id);
               // Update
                Class t = new Class(iGuid, Guid.Parse(Session["SchoolYearID"].ToString()),
           Guid.Parse(ddlKhoi.SelectedValue), txtMaLop.Value, txtTenLop.Value, chk2DoTuoi.Checked, chkCTGDMoi.Checked, chk3Maugiao.Checked, false, false, false, false, chk24.Checked, chk36.Checked, chk4.Checked, chk5.Checked, chk6.Checked, int.Parse(Session["SchoolID"].ToString()));

                _ClassRepository.Update(t);
                Alert.Show("Cập nhật dữ liệu thành công!");
                Alert.ClodePopup();


            }
            if (action == "'them'")
            {
              //  AddNew
                _ClassRepository.Insert(new Class(Guid.NewGuid(), Guid.Parse(Session["SchoolYearID"].ToString()),
           Guid.Parse(ddlKhoi.SelectedValue), txtMaLop.Value, txtTenLop.Value, chk2DoTuoi.Checked, chkCTGDMoi.Checked, chk3Maugiao.Checked, false, false, false, false, chk24.Checked, chk36.Checked, chk4.Checked, chk5.Checked, chk6.Checked, int.Parse(Session["SchoolID"].ToString())));
                Alert.Show("Thêm mới dữ liệu thành công!");
                Alert.ClodePopup();
            }
        }
        else
        {
            Alert.Show("Thông tin cần nhập chưa đầy đủ !");
        }

    }
}